#include "shapewidget.h"
#include <QPixmap>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>

ShapeWidget::ShapeWidget(QWidget *parent)
    : QWidget(parent)
{
    QPixmap pix; //新建一个 QPixmap 对象
    pix.load("16.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither); //(a)
    resize (pix. size()); // (b)
    setMask(QBitmap(pix.mask())); //(c)
}

ShapeWidget::~ShapeWidget()
{
}

void ShapeWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition = event->globalPosition()-geometry().topLeft();
        event->accept();
    }
    if (event->button ()==Qt::RightButton)
    {
        close();
    }
}

void ShapeWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton)
    {
        move((event->globalPosition() - dragPosition).x(),(event->globalPosition() - dragPosition).y());
        event->accept ();
    }
}

void ShapeWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,QPixmap("16.png"));
}

